﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.SqlClient;


namespace OF.Utility
{
    [Serializable]

    ///<summary>
    ///Site_VisitLog 业务实体类 
    ///</summary>
    public class Site_VisitLogInfo
    {

        #region 内部成员变量

        private int _intMemberID;
        private int _intInfoID;
        private string _strTypeID;
        private string _strIp;
        private string _strBrowser;
        private string _strOs;
        private string _strLanguage;
        private string _strScreen;
        private string _strCookie;
        private DateTime _strVisitTime;
        private string _strReferer;
        private string _strLocationURL;
        private string _strSn;
        private int _intSite;

        #endregion

        #region 属性
        ///<summary>
        ///MemberID
        ///</summary>
        public int SiteID
        {
            get { return _intSite; }
            set { _intSite = value; }
        }

        ///<summary>
        ///MemberID
        ///</summary>
        public int MemberID
        {
            get { return _intMemberID; }
            set { _intMemberID = value; }
        }
        ///<summary>
        ///InfoID
        ///</summary>
        public int InfoID
        {
            get { return _intInfoID; }
            set { _intInfoID = value; }
        }
        ///<summary>
        ///TypeID
        ///</summary>
        public string TypeID
        {
            get { return _strTypeID; }
            set { _strTypeID = value; }
        }
        ///<summary>
        ///Ip
        ///</summary>
        public string Ip
        {
            get { return _strIp; }
            set { _strIp = value; }
        }
        ///<summary>
        ///Browser
        ///</summary>
        public string Browser
        {
            get { return _strBrowser; }
            set { _strBrowser = value; }
        }
        ///<summary>
        ///Os
        ///</summary>
        public string Os
        {
            get { return _strOs; }
            set { _strOs = value; }
        }
        ///<summary>
        ///Language
        ///</summary>
        public string Language
        {
            get { return _strLanguage; }
            set { _strLanguage = value; }
        }
        ///<summary>
        ///Screen
        ///</summary>
        public string Screen
        {
            get { return _strScreen; }
            set { _strScreen = value; }
        }
        ///<summary>
        ///Cookie
        ///</summary>
        public string Cookie
        {
            get { return _strCookie; }
            set { _strCookie = value; }
        }
        ///<summary>
        ///VisitTime
        ///</summary>
        public DateTime VisitTime
        {
            get { return _strVisitTime; }
            set { _strVisitTime = value; }
        }
        ///<summary>
        ///Referer
        ///</summary>
        public string Referer
        {
            get { return _strReferer; }
            set { _strReferer = value; }
        }
        ///<summary>
        ///LocationURL
        ///</summary>
        public string LocationURL
        {
            get { return _strLocationURL; }
            set { _strLocationURL = value; }
        }
        ///<summary>
        ///Sn
        ///</summary>
        public string Sn
        {
            get { return _strSn; }
            set { _strSn = value; }
        }

        #endregion

    }

    public class LogUtil
    {
        private static Queue<Site_VisitLogInfo> queueAutoSiteVisit = null;
        private static Queue<Site_VisitLogInfo> queueVendorVisit = null;
        private static readonly object objQueueAutoSiteVisit = new object();
        private static readonly object objQueueVendorVisit = new object();
        //锁对象
        private static readonly object m_lockVenderQueue = new object();
        private static readonly object m_lockAutoSiteQueue = new object();

        private static int QueueCount
        {
            get
            {
                return Convert.ToInt32(ConfigurationManager.AppSettings["QueueCount"].ToString());
            }
        }
        public static int DataTableCount
        {
            get
            {
                return Convert.ToInt32(ConfigurationManager.AppSettings["DataTableCount"].ToString());
            }
        }
        /// <summary>
        /// 经销商浏览日志入队
        /// </summary>
        /// <param name="vendorVisit"></param>
        public static void InsertVendorVisitQueue(Site_VisitLogInfo vendorVisit)
        {
            QueueVendorVisit.Enqueue(vendorVisit);
            if (IsCanInsertVenderTable)
            {
                lock (m_lockVenderQueue)
                {
                    InsertVendorVisitDataTable();
                }
            }
        }
        /// <summary>
        /// 是否可以入库
        /// </summary>
        /// <returns></returns>
        private static bool IsCanInsertVenderTable
        {
            get
            {
                return QueueVendorVisit.Count >= QueueCount;
            }
        }
        /// <summary>
        /// 经销商浏览日志放入缓存
        /// </summary>
        private static void InsertVendorVisitDataTable()
        {
            if (!IsCanInsertVenderTable)
            {
                return;
            }
            DataTable dt = CreateVendorVisitTable();
            for (int i = 0; i < QueueCount; i++)
            {
                Site_VisitLogInfo vendorVisit = QueueVendorVisit.Dequeue();
                if (vendorVisit == null)
                {
                    continue;
                }
                DataRow dr = dt.NewRow();
                dr["InfoID"] = vendorVisit.InfoID;
                dr["Ip"] = vendorVisit.Ip;
                dr["TypeID"] = EConvert.ToInt(vendorVisit.TypeID);
                dr["Language"] = vendorVisit.Language;
                dr["LocationURL"] = vendorVisit.LocationURL;
                dr["Os"] = vendorVisit.Os;
                dr["Referer"] = vendorVisit.Referer;
                dr["Screen"] = vendorVisit.Screen;
                dr["Sn"] = vendorVisit.Sn;
                dr["MemberID"] = vendorVisit.MemberID;
                dr["VisitTime"] = vendorVisit.VisitTime;
                dr["Cookie"] = vendorVisit.Cookie;
                dr["Browser"] = vendorVisit.Browser;
                dr["SiteID"] = vendorVisit.SiteID;
                dt.Rows.Add(dr);
            }
            InsertVenderVisitLog(dt);
        }
        /// <summary>
        /// 经销商浏览日志入库
        /// </summary>
        private static void InsertVenderVisitLog(DataTable dt)
        {
            using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["EJAutoBaseRW"].ConnectionString))
            {
                cnn.Open();
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(cnn))
                {
                    sqlBulkCopy.DestinationTableName = "Site_VisitLog";
                    sqlBulkCopy.ColumnMappings.Add("InfoID", "InfoID");
                    sqlBulkCopy.ColumnMappings.Add("TypeID", "TypeID");
                    sqlBulkCopy.ColumnMappings.Add("MemberID", "MemberID");
                    sqlBulkCopy.ColumnMappings.Add("Ip", "Ip");
                    sqlBulkCopy.ColumnMappings.Add("Language", "Language");
                    sqlBulkCopy.ColumnMappings.Add("LocationURL", "LocationURL");
                    sqlBulkCopy.ColumnMappings.Add("Os", "Os");
                    sqlBulkCopy.ColumnMappings.Add("Referer", "Referer");
                    sqlBulkCopy.ColumnMappings.Add("Browser", "Browser");
                    sqlBulkCopy.ColumnMappings.Add("Cookie", "Cookie");
                    sqlBulkCopy.ColumnMappings.Add("Screen", "Screen");
                    sqlBulkCopy.ColumnMappings.Add("Sn", "Sn");
                    sqlBulkCopy.ColumnMappings.Add("VisitTime", "VisitTime");
                    sqlBulkCopy.ColumnMappings.Add("SiteID", "SiteID");
                    try
                    {
                        sqlBulkCopy.WriteToServer(dt);

                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        /// <summary>
        /// 旺店日志入队
        /// </summary>
        /// <param name="autoSiteVisit"></param>
        public static void InsertAutoSiteVisitQueue(Site_VisitLogInfo autoSiteVisit)
        {
            QueueAutoSiteVisit.Enqueue(autoSiteVisit);
            if (IsCanInsertAutoSiteTable)
            {
                lock (m_lockAutoSiteQueue)
                {
                    InsertAutoSiteDataTable();
                }
            }
        }
        private static bool IsCanInsertAutoSiteTable
        {
            get
            {
                return QueueAutoSiteVisit.Count >= QueueCount;
            }
        }
        /// <summary>
        /// 旺店日志存入缓存
        /// </summary>
        private static void InsertAutoSiteDataTable()
        {
            if (!IsCanInsertAutoSiteTable)
            {
                return;
            }
            DataTable dt = CreateAutoSiteVisitTable();
            for (int i = 0; i < QueueCount; i++)
            {
                Site_VisitLogInfo autoSiteVisit = QueueAutoSiteVisit.Dequeue();
                if (autoSiteVisit == null)
                {
                    continue;
                }
                DataRow dr = dt.NewRow();
                dr["InfoID"] = autoSiteVisit.InfoID;
                dr["Ip"] = autoSiteVisit.Ip;
                dr["Language"] = autoSiteVisit.Language;
                dr["LocationURL"] = autoSiteVisit.LocationURL;
                dr["Os"] = autoSiteVisit.Os;
                dr["Referer"] = autoSiteVisit.Referer;
                dr["Screen"] = autoSiteVisit.Screen;
                dr["Sn"] = autoSiteVisit.Sn;
                dr["TypeID"] = autoSiteVisit.TypeID;
                dr["MemberID"] = autoSiteVisit.MemberID;
                dr["VisitTime"] = autoSiteVisit.VisitTime;
                dr["Cookie"] = autoSiteVisit.Cookie;
                dr["Browser"] = autoSiteVisit.Browser;
                dr["SiteID"] = autoSiteVisit.SiteID;
                dt.Rows.Add(dr);
            }
            InsertAutoSiteVisitLog(dt);
 
        }
        /// <summary>
        /// 旺店日志入库
        /// </summary>
        private static void InsertAutoSiteVisitLog(DataTable dt)
        {
            using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["EJAutoBaseRW"].ConnectionString))
            {
                cnn.Open();
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(cnn))
                {
                    sqlBulkCopy.DestinationTableName = "[DealerStatisticsSystem].[dbo].[AutoSiteVisitLog]";
                    sqlBulkCopy.ColumnMappings.Add("InfoID", "InfoID");
                    sqlBulkCopy.ColumnMappings.Add("TypeID", "TypeID");
                    sqlBulkCopy.ColumnMappings.Add("Ip", "Ip");
                    sqlBulkCopy.ColumnMappings.Add("Language", "Language");
                    sqlBulkCopy.ColumnMappings.Add("LocationURL", "LocationURL");
                    sqlBulkCopy.ColumnMappings.Add("Os", "Os");
                    sqlBulkCopy.ColumnMappings.Add("Referer", "Referer");
                    sqlBulkCopy.ColumnMappings.Add("Browser", "Browser");
                    sqlBulkCopy.ColumnMappings.Add("Cookie", "Cookie");
                    sqlBulkCopy.ColumnMappings.Add("Screen", "Screen");
                    sqlBulkCopy.ColumnMappings.Add("Sn", "Sn");
                    sqlBulkCopy.ColumnMappings.Add("TypeID", "TypeID");
                    sqlBulkCopy.ColumnMappings.Add("MemberID", "MemberID");
                    sqlBulkCopy.ColumnMappings.Add("VisitTime", "VisitTime");
                    sqlBulkCopy.ColumnMappings.Add("SiteID", "SiteID");
                    try
                    {
                        sqlBulkCopy.WriteToServer(dt);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        /// <summary>
        /// 旺店日志队列
        /// </summary>
        public static Queue<Site_VisitLogInfo> QueueAutoSiteVisit
        {
            get
            {
                if (queueAutoSiteVisit == null)
                {
                    lock (objQueueAutoSiteVisit)
                    {
                        queueAutoSiteVisit = new Queue<Site_VisitLogInfo>();
                    }
                }
                return queueAutoSiteVisit;
            }
        }
        /// <summary>
        /// 经销商日志队列
        /// </summary>
        public static Queue<Site_VisitLogInfo> QueueVendorVisit
        {
            get
            {
                if (queueVendorVisit == null)
                {
                    lock (objQueueVendorVisit)
                    {
                        queueVendorVisit = new Queue<Site_VisitLogInfo>();
                    }
                }
                return queueVendorVisit;
            }
        }
        /// <summary>
        /// 创建旺店缓存表
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateAutoSiteVisitTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("InfoID", typeof(int));
            dt.Columns.Add("ModuleID", typeof(int));
            dt.Columns.Add("Ip", typeof(string));
            dt.Columns.Add("Browser", typeof(string));
            dt.Columns.Add("Os", typeof(string));
            dt.Columns.Add("Language", typeof(string));
            dt.Columns.Add("Screen", typeof(string));
            dt.Columns.Add("Cookie", typeof(int));
            dt.Columns.Add("VisitTime", typeof(DateTime));
            dt.Columns.Add("Referer", typeof(string));
            dt.Columns.Add("LocationURL", typeof(string));
            dt.Columns.Add("Sn", typeof(string));
            dt.Columns.Add("MemberID", typeof(int));
            dt.Columns.Add("SiteID", typeof(int));
            dt.Columns.Add("TypeID", typeof(int));
            dt.Columns.Add("SiteID", typeof(int));
           
            return dt;
        }
        /// <summary>
        /// 创建经销商缓存表
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateVendorVisitTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("InfoID", typeof(int));
            dt.Columns.Add("TypeID", typeof(int));
            dt.Columns.Add("Ip", typeof(string));
            dt.Columns.Add("Browser", typeof(string));
            dt.Columns.Add("Os", typeof(string));
            dt.Columns.Add("Language", typeof(string));
            dt.Columns.Add("Screen", typeof(string));
            dt.Columns.Add("Cookie", typeof(string));
            dt.Columns.Add("VisitTime", typeof(DateTime));
            dt.Columns.Add("Referer", typeof(string));
            dt.Columns.Add("LocationURL", typeof(string));
            dt.Columns.Add("Sn", typeof(string));
            dt.Columns.Add("MemberID", typeof(int));
            dt.Columns.Add("SiteID", typeof(int));
     
            return dt;
        }
    }
}